package com.edu.scau.dao.implement;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;

import javax.annotation.*;

import com.edu.scau.dao.ICommodityDao;
import com.edu.scau.dao.util.CommodityPage;
import com.edu.scau.entity.Commodity;

@Component
public class CommodityDaoImpl implements ICommodityDao {
	
	@Resource(name = "sessionFactory")
	private SessionFactory factory;
	public SessionFactory getFactory() {
		return factory;
	}

	public void setFactory(SessionFactory factory) {
		this.factory = factory;
	}

	
	//==============
	public CommodityDaoImpl() {
		super();
	}

	public CommodityDaoImpl(SessionFactory factory) {
		super();
		this.factory = factory;
	}
	//=================
	/*
	 * 方法说明:增加一个商品信息
	 */
	public int addCommodity(Commodity commodity) {
		return 0;
	}

	/*
	 * 方法说明:删除一个商品信息
	 */
	public int deleteCommodity(Commodity commodity) {
		return 0;
	}

	/*
	 * 方法说明:更新一个商品信息
	 */
	public int updataCommodity(Commodity commodity) {
		int row=1;
		Session session=null; //定义Session
		Transaction tx=null;  // 定义事务
		try {
			session=factory.getCurrentSession();
			session.update(commodity);
		} catch (HibernateException e) {
			e.printStackTrace();
			return 0;
		}
		return row;
	}
	//======================================
	/*
	 * 方法说明:查询全部商品信息
	 */
	@SuppressWarnings("unchecked")
	public List<Commodity> queryCommodityAll(CommodityPage page) {
		List<Commodity> list=null;
		try {
			Session session=factory.getCurrentSession();
			Criteria criteria=session.createCriteria(Commodity.class);
			criteria.setFirstResult(page.getBeginIndex());
			criteria.setMaxResults(page.getEveryPage());
			list=criteria.list();
		} catch (HibernateException e) {
			e.printStackTrace();
		}
		return list;
	}

	public int queryNumCommodityAll() {
		int row=0;
		try {
			Session session=factory.getCurrentSession();
			Criteria criteria=session.createCriteria(Commodity.class);
			row=criteria.list().size();
		} catch (HibernateException e) {
			e.printStackTrace();
		}
		return row;
	}

	
	/*
	 * 方法说明:根据商品分类(Classification)查询商品信息
	 */
	@SuppressWarnings("unchecked")
	public List<Commodity> queryCommodityByClassification(CommodityPage page,
			String classification) {
		List<Commodity> list=null;
		try {
			Session session=factory.getCurrentSession();
			Criteria criteria=session.createCriteria(Commodity.class);
			criteria.add(Restrictions.eq("classification",classification));
			criteria.setFirstResult(page.getBeginIndex());
			criteria.setMaxResults(page.getEveryPage());
			list=criteria.list();
		} catch (HibernateException e) {
			e.printStackTrace();
		}
		return list;
	}

	public int queryNumCommodityByClassification(String classification) {
		int row=0;
		try {
			Session session=factory.getCurrentSession();
			Criteria criteria=session.createCriteria(Commodity.class);
			criteria.add(Restrictions.eq("classification",classification));
			row=criteria.list().size();
		} catch (HibernateException e) {
			e.printStackTrace();
		}
		return row;
	}

	/*
	 * 方法说明:根据商品编号(id)查询商品信息
	 */
	@SuppressWarnings("unchecked")
	public List<Commodity> queryCommodityById(CommodityPage page, int id) {
		List<Commodity> list=null;
		try {
			Session session=factory.getCurrentSession();
			Criteria criteria=session.createCriteria(Commodity.class);
			criteria.add(Restrictions.eq("id",id));
			criteria.setFirstResult(page.getBeginIndex());
			criteria.setMaxResults(page.getEveryPage());
			list=criteria.list();
		} catch (HibernateException e) {
			e.printStackTrace();
		}
		return list;
	}
	/*
	 * 方法说明:根据商品编号(id)查询商品信息的总记录数
	 * @return 相等id的总记录数(其实是0或者1)
	 */
	public Commodity queryNumCommodityById(int id) {
		
			Session session=factory.getCurrentSession();
			Criteria criteria=session.createCriteria(Commodity.class);
			List<Commodity> commoditys = (List<Commodity>) criteria.add(Restrictions.eq("id",id)).list();
			if(commoditys.size() == 0)
				return null;
			return commoditys.get(0);
		
	}

	/*
	 * 方法说明:根据商品名字(name)查询商品信息(like)
	 * @return 名字相似的商品的列表
	 */
	@SuppressWarnings("unchecked")
	public List<Commodity> queryCommodityByName(CommodityPage page, String name) {
		List<Commodity> list=null;
		try {
			Session session=factory.openSession();
			Criteria criteria=session.createCriteria(Commodity.class);
			criteria.add(Restrictions.like("name",name));
			criteria.setFirstResult(page.getBeginIndex());
			criteria.setMaxResults(page.getEveryPage());
			list=criteria.list();
			session.close();
		} catch (HibernateException e) {
			e.printStackTrace();
		}
		return list;
	}
	/*
	 * 方法说明:根据商品名字(name)查询商品信息(like)
	 * @return 名字相似的商品的列表
	 */
	public int queryNumCommodityByName( String name) {
		int row=0;
		try {
			Session session=factory.getCurrentSession();
			Criteria criteria=session.createCriteria(Commodity.class);
			criteria.add(Restrictions.like("name",name));
			row=criteria.list().size();
		} catch (HibernateException e) {
			e.printStackTrace();
		}
		return row;
	}

	/*
	 * 方法说明:根据商品价格(price)查询商品信息
	 * @return 最低价格和最高价格之间的商品列表
	 */
	@SuppressWarnings("unchecked")
	public List<Commodity> queryCommodityByPrice(CommodityPage page,
			double beginPrice, double endPrice) {
		List<Commodity> list=null;
		try {
			Session session=factory.getCurrentSession();
			Criteria criteria=session.createCriteria(Commodity.class);
			criteria.add(Restrictions.ge("price",beginPrice));
			criteria.add(Restrictions.le("price", endPrice));
			criteria.setFirstResult(page.getBeginIndex());
			criteria.setMaxResults(page.getEveryPage());
			list=criteria.list();
		} catch (HibernateException e) {
			e.printStackTrace();
		}
		return list;
	}
	/*
	 * 方法说明:根据商品价格(price)查询商品信息
	 * @return 最低价格和最高价格之间的商品总数
	 */
	public int queryNumCommodityByPrice(double beginPrice,double endPrice) {
		int row=0;
		try {
			Session session=factory.getCurrentSession();
			Criteria criteria=session.createCriteria(Commodity.class);
			criteria.add(Restrictions.ge("price",beginPrice));
			criteria.add(Restrictions.le("price", endPrice));
			row=criteria.list().size();
		} catch (HibernateException e) {
			e.printStackTrace();
		}
		return row;
	}

	/*
	 * 方法说明:根据商品描述(description)查询商品信息(模糊查找)
	 * @return  描述中存在关键字的商品列表
	 */
	@SuppressWarnings("unchecked")
	public List<Commodity> queryCommodityByLikeName(CommodityPage page,
			String likeName) {
		List<Commodity> list=null;
		try {
			Session session=factory.getCurrentSession();
			Criteria criteria=session.createCriteria(Commodity.class);
			criteria.add(Restrictions.like("description",likeName));
			criteria.setFirstResult(page.getBeginIndex());
			criteria.setMaxResults(page.getEveryPage());
			list=criteria.list();
		} catch (HibernateException e) {
			e.printStackTrace();
		}
		return list;
	}

	
	/*
	 * 方法说明:根据商品描述(description)查询商品信息(模糊查找)
	 * @return  描述中存在关键字的商品总数
	 */
	public int queryNumCommodityByLikeName(String likeName) {
		int row=0;
		try {
			Session session=factory.getCurrentSession();
			Criteria criteria=session.createCriteria(Commodity.class);
			criteria.add(Restrictions.like("description",likeName));
			row=criteria.list().size();
		} catch (HibernateException e) {
			e.printStackTrace();
		}
		return row;
	}

	public List<Commodity> queryCommodityByClassification(String classification) {
		// TODO Auto-generated method stub
		Session session=factory.getCurrentSession();
		Criteria criteria=session.createCriteria(Commodity.class);
		@SuppressWarnings("unchecked")
		List<Commodity> lsCommodities= criteria.add(Restrictions.like("classification","%"+classification+"%")).list();
		return lsCommodities;
		
		
		
	}


}
